#include "renaming.h"

Renaming::Renaming(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Btn_Init();
   ui_Label_Init();
   connect_Init();
}

bool Renaming::eventFilter(QObject *watched, QEvent *event)
{
    if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched)
{
    if(event->type() == QEvent::KeyPress)
    {
        //qDebug()<<focusWidget()->objectName();
        QKeyEvent *ev = (QKeyEvent *)event;
        QPushButton *button = (QPushButton *)watched;
       {
           switch (ev->key())
           {
           case OK:
           {

               emit  button-> clicked();
             break;
           }
           case UP: //Up
             {
//                         if(focusWidget() != btnlist_ui.first())
                    focusPreviousChild();
               break;
             }
           case DOWN: //Down
             {
//                         if(focusWidget() != btnlist_ui.back())
                    focusNextChild();
               break;
             }
           case BACK:
           {
               this->parentWidget()->show();
               this->deleteLater();
               break;
           }
           case INIT:
           {
               init_Show *show = new init_Show(this);
               show->show();
               this->hide();
               break;
           }
            default:
               break;
           }
       }
    }
//    else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
//              {
//                  QPushButton *focus_button = (QPushButton *)watched;
//                          focus_button->setFixedSize(160,120);
////                                  focus_button->setStyleSheet(focusBtn[2]->styleSheet());
////                                  focus_button->setIconSize(QSize(1,2));
//              }
//    else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
//             {
//        QPushButton *focus_button = (QPushButton *)watched;
//                focus_button->setFixedSize(120,90);
//             }
    }
    return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}

void Renaming::ui_Label_Init()
{
    for(int i=0;i<Label_Num;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #123567;			                 /* 文本颜色 */\
                           }"
          );
    }
    theLabel[0]->setGeometry(50,150,200,40);//设定每隔放置一个按键
    theLabel[1]->setGeometry(300,150,100,40);//设定每隔放置一个按键
    theLabel[0]->setText(tr("当前气体名称为："));
    theLabel[1]->setText(tr(""));
    theLabel[1]->setText(QString(Sysset->Gas[GasSet_ABC-1].gasname));
    theLabel[2]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[3]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[4]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[2]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[3]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[4]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[2]->setText("气体名称修改");

    }

    void Renaming::ui_Btn_Init()
    {
        for(int i=0;i<2;i++)//创建主屏幕显示的按键
        {

             theBtn[i] = new QPushButton(this);
    //        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
            theBtn[i]->setFixedSize(120,90);
            theBtn[i]->geometry();
            theBtn[i]->setStyleSheet("QPushButton{\
                                   outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                   background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                   border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                   border-style: outset;\
                                   /* 按钮背景图标设置 */\
                                   background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                                   background-origin: content;\
                                   background-position: top;	         /* 背景图片的位置 */\
                                   padding-right: 0px;	    /*内边距设置*/	    \
                                   padding-left: 0px;		/*  */	\
                                   padding-bottom: 10px;		/*  */    \
                                   padding-top: 10px;		/*  */    \
                                   background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                                   /* 按钮文本设置 */\
                                   text-align: bottom;			             /* 文本的位置 */\
                                   font-size: 20px;\
                                   color: #FFFFFF;			                 /* 文本颜色 */\
                              }"
        "QPushButton:focus{\
    outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
        background-color: rgb(255,125,25);	    /* 背景颜色 */\
        border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
        border-style: outset;\
        /* 按钮背景图标设置 */\
        background-image: url(:/pic/access/过滤.png); /* 背景图片 */\
        background-origin: content;\
        background-position: top;	         /* 背景图片的位置 */\
        padding-right: 0px;	    /*内边距设置*/	    \
        padding-left: 0px;		/*  */	\
        padding-bottom: 10px;	/*  */   \
        padding-top: 10px;		/*  */    \
        background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
        /* 按钮文本设置 */\
        text-align: bottom;			             /* 文本的位置 */\
        font-size: 22px;\
        qproperty-iconSize: 20px 20px;			/* 设置图标大小*/\
    color: rgb(125, 255, 250);			                 /* 文本颜色 */\
    }"
    "QPushButton:pressed{background-color:rgb(99,211,133);}"
    "QPushButton:checked{background-color:rgb(66,66,99);}"
    //"QPushButton:hover{background-color:rgb(255,111,111);}"
    //"QPushButton:MouseButtonPress{background-color:rgb(255,111,111);}"
        );
    //        newbutton->setIconSize(QSize(1,2));

    }
    btnlist_ui.append(theBtn[0]);
    btnlist_ui.append(theBtn[1]);
    theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
     "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
    theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
     "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

    btnlist_ui.at(0)->setGeometry(150,300,120,90 );
    btnlist_ui.at(1)->setGeometry(360,300,120,90 );
    btnlist_ui.at(0)->setText(tr("更改名称"));
    btnlist_ui.at(1)->setText(tr("返回"));

    //事件过滤器
    foreach (QPushButton *btn, btnlist_ui) {
        btn->installEventFilter(this);
    }
    }

    void Renaming::connect_Init()
    {
        //确定
        connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){

            SoftKeyboard *board = new SoftKeyboard();
            //softKeyboard->setTitleLabelText("please input:");
            //设置模态阻塞
      //      board->setWindowModality(Qt::WindowModal);
            board->show();
            connect(board,&SoftKeyboard::sendText,this,&Renaming::rename);
                connect(board,&SoftKeyboard::destroyed,[=](){
                btnlist_ui.at(0)->setFocus();
                        });
        });

        //返回
        connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
//            emit sendText(currentLineEdit->text());

            this->parentWidget()->show();
            this->close();
        });
    }

    void Renaming::rename(QString text)
    {
 theLabel[1]->setText( text);
 emit sendText(text);
 Gas_Selname[GasSet_ABC-1] = text;
 qstrcpy(Sysset->Gas[GasSet_ABC-1].gasname,Gas_Selname[GasSet_ABC-1].toUtf8().data());

    }
